<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/12/10
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%@ page import="java.sql.*" %>
<%@ page import="com.example.entity.User" %>

<%
    HttpSession session1 = request.getSession();
    User user1 = (User) session1.getAttribute("user");
    String flag = request.getParameter("flag");
    int userid = user1.getUserId();
    String userimg = request.getParameter("userimg");
    String username = request.getParameter("username");
    String gender = request.getParameter("gender");
    String signature = request.getParameter("signature");
    user1.setUserimg(userimg);
    user1.setUsername(username);
    user1.setGender(gender);
    user1.setSignature(signature);
//    System.out.println(flag);


    // 数据库连接信息
    String url = "jdbc:mysql://localhost:3306/db1";
    String user = "root";
    String password = "123456";

    try {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 建立连接
        Connection conn = DriverManager.getConnection(url, user, password);
//        System.out.println("conn = " + conn);
        String sql;
        PreparedStatement pstmt;
        // 创建SQL语句
        sql = "UPDATE userinfo SET USERIMG = ?,USERNAME = ?,GENDER = ?,SIGNATURE = ? WHERE USER_ID = ?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, userimg);
        pstmt.setString(2, username);
        pstmt.setString(3, gender);
        pstmt.setString(4, signature);
        pstmt.setInt(5, userid);

        session.setAttribute("user", user1);


        // 执行SQL语句
        pstmt.executeUpdate();

        // 关闭连接
        pstmt.close();
        conn.close();

        out.println("内容已成功保存到数据库");
    } catch (Exception e) {
        e.printStackTrace();
        out.println("保存内容失败: " + e.getMessage());
    }
%>
</body>
</html>
